﻿/*********************************************
*Create By：Paine Zeng
*Create Date: 2023/08/20 21:25:50
*Description:
*Email: tozeng@vip.qq.com
*Blog:  https://blog.csdn.net/zengqh0314
*Gitee: https://gitee.com/painezeng
**********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;

namespace SolidWorks.Base
{

    /// <summary>
    /// 属性对象
    /// </summary>
    public class PropertiesHelper
    {
        /// <summary>
        /// 获取模型的所有配置
        /// </summary>
        /// <param name="swModel"></param>
        public static List<PropertyInfo> GetModelProperties(ModelDoc2 swModel)
        {

            var allProperties = new List<PropertyInfo>();

            //获取自定义属性
            var tempPropertyInfo = GetSpecConfigPropertyInfo(swModel, "");

            allProperties.Add(tempPropertyInfo);
            
            //不是工程图，跳过

            if (swModel.GetType()!=(int)swDocumentTypes_e.swDocDRAWING)
            {
                var ConfigNames = (string[])swModel.GetConfigurationNames(); //所有配置名称

                foreach (var configName in ConfigNames)//遍历所有配置
                {

                    tempPropertyInfo = GetSpecConfigPropertyInfo(swModel, configName);
                    allProperties.Add(tempPropertyInfo);

                }
            }


            return allProperties;


        }

        /// <summary>
        /// 获取指定配置的属性信息
        /// </summary>
        /// <param name="swModel">模型对象</param>
        /// <param name="activeConfigName">配置名称</param>
        /// <returns></returns>
        public static PropertyInfo GetSpecConfigPropertyInfo(ModelDoc2 swModel, string activeConfigName)
        {
            var tempPropertyInfo = new PropertyInfo
            {
                ConfigName = activeConfigName
            };

            var cusPropMgr = swModel.Extension.CustomPropertyManager[activeConfigName];

            object propNames = null;
            object propTypes = null;
            object propValues = null;
            object propResolved = null;

            var propCount = cusPropMgr.GetAll2(ref propNames, ref propTypes, ref propValues, ref propResolved);

            if (propCount > 0)
            {
                var propNamesList = (object[])propNames;
                var propTypesList = (Int32[])propTypes;
                var propValuesList = (string[])propValues;
                var propResolvedList = (Int32[])propResolved;

                for (int i = 0; i < propCount; i++)
                {
                    var tempSWProp = new PropertyItem();
                    tempSWProp.ItemType = (swCustomInfoType_e)propTypesList[i];

                    tempSWProp.ItemValue = propValuesList[i].ToString();
                    tempSWProp.ItemName = propNamesList[i].ToString();
                    tempPropertyInfo.PropertyItems.Add(tempSWProp);
                }
            }

            return tempPropertyInfo;
        }

        /// <summary>
        /// 获取类型名称
        /// </summary>
        /// <param name="type_e"></param>
        /// <returns></returns>
        public static string GetTypeNameForPropertyType(int type_e)
        {
            if (type_e == 64)
            {
                return "日期";
            }
            if (type_e == 5)
            {
                return "数字";
            }
            if (type_e == 3)
            {
                return "数字";
            }
            if (type_e == 11)
            {
                return "是或否";
            }
            if (type_e == 30)
            {
                return "文字";
            }
            return "未知";
        }
        /// <summary>
        /// 类型转文字
        /// </summary>
        /// <param name="swCustomInfoType"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static string GetTypeNameForPropertyType(swCustomInfoType_e swCustomInfoType)
        {
            switch (swCustomInfoType)
            {
                case swCustomInfoType_e.swCustomInfoUnknown:
                    return "未知";
                    
                case swCustomInfoType_e.swCustomInfoText:
                    return "文字";
            
                case swCustomInfoType_e.swCustomInfoDate:
                    return "日期";
             
                case swCustomInfoType_e.swCustomInfoNumber:
                    return "数字";
          
                case swCustomInfoType_e.swCustomInfoDouble:
                    return "数字";
            
                case swCustomInfoType_e.swCustomInfoYesOrNo:
                    return "是或否";
                
                default:
                    throw new ArgumentOutOfRangeException(nameof(swCustomInfoType), swCustomInfoType, null);
            }

        }

        /// <summary>
        /// 修改属性值
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="activeConfigName"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <param name="DeleteFirst">是否删除</param>
        /// <returns></returns>
        public static bool ModifyCustomProperty(ModelDoc2 swModel, string activeConfigName, string name, string value,int type,bool DeleteFirst)
        {
            var res = 0;
           var swCustomProp = swModel.Extension.CustomPropertyManager[activeConfigName];

           if (DeleteFirst)
           {
                res = swCustomProp.Add3(name, type, value, (int)swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd);

           }
           else
           {
                res = swCustomProp.Add3(name, type, value, (int)swCustomPropertyAddOption_e.swCustomPropertyReplaceValue);

           }

           return res == 0;

        }

        /// <summary>
        /// 删除属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="activeConfigName"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool DeleteCustomProperty(ModelDoc2 swModel, string activeConfigName, string name)
        {

            var swCustomProp = swModel.Extension.CustomPropertyManager[activeConfigName];

            var res = swCustomProp.Delete2(name);

            return res == 0;

        }

        /// <summary>
        /// 中文转Type
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static swCustomInfoType_e GetCustomInforTypeByName(string name)
        {

            swCustomInfoType_e resType;


            switch (name)
            {
                case "未知" :
                    resType= swCustomInfoType_e.swCustomInfoUnknown;
                    break;

                case "文字":
                    resType=swCustomInfoType_e.swCustomInfoText;
                    break;
                case "日期" :
                    resType=swCustomInfoType_e.swCustomInfoDate;
                    break;
                case "数字":
                    resType = swCustomInfoType_e.swCustomInfoDouble;
                    break;
                case "是或否":
                    resType = swCustomInfoType_e.swCustomInfoYesOrNo;
                    break;
                default:
                    resType = swCustomInfoType_e.swCustomInfoText;
                    break;
            }

            return resType;

        }
    }


    /// <summary>
    /// 配置的属性信息
    /// </summary>
    public class PropertyInfo
    {
        /// <summary>
        /// 配置名称，自定义的话为空
        /// </summary>
        public string  ConfigName { get; set; }
        /// <summary>
        /// 该配置下所有属性值列表
        /// </summary>
        public List<PropertyItem> PropertyItems { get; set; }= new List<PropertyItem>();

    }

    /// <summary>
    /// 属性项
    /// </summary>
    public class PropertyItem
    {
        /// <summary>
        /// 属性名称
        /// </summary>
        public string ItemName { get; set; }
        /// <summary>
        /// 属性类型
        /// </summary>
        public swCustomInfoType_e ItemType { get; set; }
        /// <summary>
        /// 属性值
        /// </summary>
        public string ItemValue { get; set; }

        /// <summary>
        /// 是否是链接,sw2018开始有用
        /// </summary>
        public bool IsLinked { get; set; }

    }


}
